<?php
namespace app\wxapp\controller;

use app\model\Theme; 
use app\model\Bargain; 

class LtBargain extends Base
{
    public function Lt_bargain()
    {
/**
 * Created by 砍价
 * Desc:微信小程序砍价
 * User:cbw
 * Date: 2020/12/7
 */
global $_P, $_G;
$response = modelShow(40000);
yxlog($_P);
yxlog("当前UID:" . $_G['member']['uid']);
if (!$_G['ispost']){
    $response = modelShow(50000);
}else{
    $op = isset($_P['op'])?$_P['op']:'display'; //display默认   order 查找对应的砍价实例   help 帮砍查询 search_bargain
    $order_id = isset($_P['recordid'])?$_P['recordid']:'';
    $uid = $_G['member']['uid'];
    $themeid = isset($_P['themeid'])?$_P['themeid']:'';
    $from_uid = isset($_P['fromuid'])?$_P['fromuid']:'';
    $op_type = isset($_P['op_type'])?$_P['op_type']:'';//system share share_double friend //曦尊定制
    $b_id = isset($_P['b_id'])?$_P['b_id']:'';
    if ($op=='display'){
//        $from_uid = empty($from_uid)?0:$from_uid;
        $from_uid = empty($from_uid)?$uid:$from_uid;
    }else{
        $from_uid = empty($from_uid)?$uid:$from_uid;
    }


    //基本检测
    $fans = $this->checkuser();
    $uid = $fans['uid'];
    $system=$_G['system'];
    $theme = Theme::getThemeById($themeid);
    $config =   Theme::getConfig($themeid);

    //判断活动时间是否已经结束 拼团模式是否开启
    $check_theme = Bargain::checkTheme($system,$theme);
    $error_text = "";
    if ($check_theme==2){
        $error_text = modelShow(40070);
    }
    if ($check_theme==3 || $theme['status']==0){
        $error_text = modelShow(40064);
    }
    if ($check_theme==4 || $theme['status']==2){
        $error_text = modelShow(40065);
    }
    $grid_tips = Bargain::getShopGridTips($theme);//获取店铺黄金宝刀提示语
    //获取当前活动的真实帮砍人数
    $resal_theme_help_total = Bargain::getFriendFlagByThemeid($themeid);

    $remain_entry_total = 100;//剩余库存
    $set_entry_total = $config['bargain_limit'];//限制库存
    //获取已完成\进行中的砍价的数量
    $default_entry_total = Bargain::getBargainEntryDefaultCount($themeid);
    //获取成功的砍价实例数量
    $success_total = Bargain::getBargainEntryCount($themeid,1)+$config['fakejoin'];

    if ($set_entry_total>0){
        //剩余数量
        $remain_entry_total = max(0,($set_entry_total-$success_total));
    }
    //判断当前的模式 定制还是常规
    $mode = 'default';
    if ($system['bargain_gift']==1){
        $mode = 'bargain_gift';//曦尊定制
    }
    $is_new_order = false;
    $is_new_order = Bargain::checkUserBuy($themeid,$uid);
    //获取已砍价用户头像列表
    $user_icon_list = Bargain::getRealAndVirtualInfo($config,$themeid);//他们都在砍
    //获取砍价排行榜
    $get_bargain_ranking = Bargain::getBargainRanking($themeid);
    if (count($get_bargain_ranking)>0){
        $bargain_ranking = $get_bargain_ranking;
    }
    if ($op=='display'){
        if ($order_id){
            $check_entry = Bargain::checkBargainEntryByOrderId($order_id,$from_uid,$themeid);
            if (!$check_entry){
                $from_uid = $uid;
            }
        }
        //获取进行中、完成、已过期的砍价实例
        $has_entry = Bargain::getUserCanUsedBargainEntry($themeid,$from_uid);
        if ($from_uid!=$uid && $from_uid!=0 && $op_type!='newBargain'){
            //help
            if ($has_entry){
                //判断砍价实例的状态
                $check_entry_status = Bargain::checkBargainStatus($system,$theme,$has_entry);
                if ($check_entry_status!=1 && $check_entry_status!=5 && $check_entry_status!=6){
                    if ($check_entry_status==2){
                        $error_text = modelShow(40070);
                    }
                    if ($check_entry_status==3){
                        $error_text = modelShow(40064);
                    }
                    if ($check_entry_status==4){
                        $error_text = modelShow(40065);
                    }
                }
                //基础信息
                $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
                $return_arr = Bargain::getHelpArr($themeid,$uid,$from_uid,$order_id,$config,$check_entry_status,$has_entry);
                //获取帮砍记录
                $get_bargain_order_flag = Bargain::getUserNotShareBargainFlagList($order_id,$from_uid,$themeid);
                $bargain_order_flag = Bargain::getFormatBargainOrderFlag($get_bargain_order_flag);
                $format_res = [
                    'error_text'=>$error_text,
                    'default_info'=>$default_info,
                    'help_bargain_info'=>$return_arr,
                    'bargain_order_flag'=>$bargain_order_flag,
                    'bargain_ranking'=>$bargain_ranking,
                    'user_icon_list'=>$user_icon_list,
                    'return_op'=>'help'
                ];
            }else{
                //获取进行中、完成、已过期的砍价实例
                $has_entry = Bargain::getUserCanUsedBargainEntry($themeid,$uid);
                $tips = '';
                if ($has_entry){
                    //order
                    //判断砍价实例的状态
                    $check_entry_status = Bargain::checkBargainStatus($system,$theme,$has_entry);
                    if ($check_entry_status!=1 && $check_entry_status!=5 && $check_entry_status!=6){
                        if ($check_entry_status==2){
                            $error_text = modelShow(40070);
                        }
                        if ($check_entry_status==3){
                            $error_text = modelShow(40064);
                        }
                        if ($check_entry_status==4){
                            $error_text = modelShow(40065);
                        }
                    }
                    //基础信息
                    $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
                    $return_arr = Bargain::getOrderArr($themeid,$order_id,$uid,$has_entry,$system,$config,$check_entry_status,$remain_entry_total);
                    //获取帮砍记录
                    $bargain_order_flag = Bargain::getFormatBargainOrderFlag($return_arr['get_bargain_order_flag']);

                    $format_res = [
                        'error_text'=>$error_text,
                        'bargain_order_flag'=>$bargain_order_flag,
                        'bargain_ranking'=>$bargain_ranking,
                        'default_info'=>$default_info,
                        'order_bargain_info'=>$return_arr['order_bargain_info'],
                        'help_bargain_info'=>[],
                        'display_bargain_info'=>[],
                        'use_grid'=>$config['bargain_self_grid']==1?$return_arr['used_grid']:false,
                        'use_share'=>$config['bargain_self_share']==1?$return_arr['used_share']:false,
                        'use_share_double'=>$config['bargain_self_dshare']==1?$return_arr['used_share_double']:false,
                        'user_icon_list'=>$user_icon_list,
                        'return_op'=>'order'
                    ];
                }else{
                    //display
                    $tips="立即发起砍价";
                    //如果有库存则开启限量购买
                    if ($set_entry_total>0){
                        //剩余数量
                        if ($remain_entry_total==0){
                            $tips='活动太火爆，已被抢空了';
                            $runing=100;
                        }else{
                            $runing = intval($success_total/$set_entry_total*100);
                        }
                    }
                    $display_bargain_info = [
                        'remain_entry_total'=>$remain_entry_total,
                        'runing'=>$runing,
                        'set_entry_total'=>$set_entry_total,
                        'tips'=>$tips
                    ];
                    //基础信息
                    $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
                    $get_bargain_order_flag = Bargain::getAllBargainFlagListLimit($themeid);
                    //获取当前活动的砍价记录
                    $bargain_order_flag = Bargain::getFormatBargainOrderFlag($get_bargain_order_flag);
                    $format_res = [
                        'error_text'=>$error_text,
                        'default_info'=>$default_info,
                        'display_bargain_info'=>$display_bargain_info,
                        'bargain_order_flag'=>$bargain_order_flag,
                        'bargain_ranking'=>$bargain_ranking,
                        'user_icon_list'=>$user_icon_list,
                        'order_bargain_info'=>[],
                        'help_bargain_info'=>[],
                        'help_todo_bargain_info'=>[],
                        'return_op'=>'display'
                    ];
                }
            }

        }elseif ($from_uid==$uid  && $op_type!='newBargain'){
            $tips = '';
            if ($has_entry){
                //order
                //判断砍价实例的状态
                $check_entry_status = Bargain::checkBargainStatus($system,$theme,$has_entry);
                if ($check_entry_status!=1 && $check_entry_status!=5 && $check_entry_status!=6){
                    if ($check_entry_status==2){
                        $error_text = modelShow(40070);
                    }
                    if ($check_entry_status==3){
                        $error_text = modelShow(40064);
                    }
                    if ($check_entry_status==4){
                        $error_text = modelShow(40065);
                    }
                }
                //基础信息
                $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
                $return_arr = Bargain::getOrderArr($themeid,$order_id,$uid,$has_entry,$system,$config,$check_entry_status,$remain_entry_total);
                //获取帮砍记录
                $bargain_order_flag = Bargain::getFormatBargainOrderFlag($return_arr['get_bargain_order_flag']);

                $format_res = [
                    'error_text'=>$error_text,
                    'bargain_order_flag'=>$bargain_order_flag,
                    'bargain_ranking'=>$bargain_ranking,
                    'default_info'=>$default_info,
                    'order_bargain_info'=>$return_arr['order_bargain_info'],
                    'help_bargain_info'=>[],
                    'display_bargain_info'=>[],
                    'use_grid'=>$config['bargain_self_grid']==1?$return_arr['used_grid']:false,
                    'use_share'=>$config['bargain_self_share']==1?$return_arr['used_share']:false,
                    'use_share_double'=>$config['bargain_self_dshare']==1?$return_arr['used_share_double']:false,
                    'user_icon_list'=>$user_icon_list,
                    'return_op'=>'order'
                ];
                //获取攻略 正常
                if ($has_entry['show_grid']=='yes' && $has_entry['show_share']=='no'){
                    //判断黄金宝刀的砍价详情
                    $flag = Bargain::getOrderSelfFlag($themeid,$uid,'system',$has_entry['order_id']);
                    $need_share_info = [
                        'b_id'=>$flag['b_id'],
                        'get_rand_amount'=>(int)$flag['sub_amount'],
                        'remain_amount'=>(int)$has_entry['remain_amount']
                    ];
                    $format_res['order_bargain_info']['need_share_info']=$need_share_info;
                }
                if ($has_entry['show_share']=='yes' && $has_entry['show_share_double']=='no'){
                    //获取分享砍价的详情
                    $flag = Bargain::getOrderSelfFlag($themeid,$uid,'share',$has_entry['order_id']);
                    $need_share_double_info = [
                        'b_id'=>$flag['b_id'],
                        'get_rand_amount'=>(int)$flag['sub_amount'],
                        'remain_amount'=>(int)$has_entry['remain_amount']
                    ];
                    $format_res['order_bargain_info']['need_share_double_info']=$need_share_double_info;
                }
                //定制
                if ($has_entry['show_grid']=='no_show' &&  $has_entry['show_share']=='no'){
                    $diff_amount = (int)$config['bargain_marketprice'] - $has_entry['remain_amount'];
                    $need_share_info = [
                        'b_id'=>0,
                        'get_rand_amount'=>(int)$diff_amount,
                        'remain_amount'=>(int)$has_entry['remain_amount']
                    ];
                    $format_res['order_bargain_info']['need_share_info']=$need_share_info;
                }
                if ($has_entry['show_grid']=='no_show' && $has_entry['show_share']=='yes' &&$has_entry['show_share_double']=='no'){
                    //获取分享砍价的详情
                    $flag = Bargain::getOrderSelfFlag($themeid,$uid,'share',$has_entry['order_id']);
                    $need_share_double_info = [
                        'b_id'=>$flag['b_id'],
                        'get_rand_amount'=>(int)$flag['sub_amount'],
                        'remain_amount'=>(int)$has_entry['remain_amount']
                    ];
                    $format_res['order_bargain_info']['need_share_double_info']=$need_share_double_info;
                }
            }else{
                //display
                $tips="立即发起砍价";
                //如果有库存则开启限量购买
                if ($set_entry_total>0){
                    //剩余数量
                    if ($remain_entry_total==0){
                        $tips='活动太火爆，已被抢空了';
                        $runing=100;
                    }else{
                        $runing = intval($success_total/$set_entry_total*100);
                    }
                }
                $display_bargain_info = [
                    'remain_entry_total'=>$remain_entry_total,
                    'runing'=>$runing,
                    'set_entry_total'=>$set_entry_total,
                    'tips'=>$tips
                ];
                //基础信息
                $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
                $get_bargain_order_flag = Bargain::getAllBargainFlagListLimit($themeid);
                //获取当前活动的砍价记录
                $bargain_order_flag = Bargain::getFormatBargainOrderFlag($get_bargain_order_flag);
                $format_res = [
                    'error_text'=>$error_text,
                    'default_info'=>$default_info,
                    'display_bargain_info'=>$display_bargain_info,
                    'bargain_order_flag'=>$bargain_order_flag,
                    'bargain_ranking'=>$bargain_ranking,
                    'user_icon_list'=>$user_icon_list,
                    'order_bargain_info'=>[],
                    'help_bargain_info'=>[],
                    'help_todo_bargain_info'=>[],
                    'return_op'=>'display'
                ];
            }
        }elseif($op_type=='newBargain'){
            //display
            $tips="立即发起砍价";
            //如果有库存则开启限量购买
            if ($set_entry_total>0){
                //剩余数量
                if ($remain_entry_total==0){
                    $tips='活动太火爆，已被抢空了';
                    $runing=100;
                }else{
                    $runing = intval($success_total/$set_entry_total*100);
                }
            }
            $display_bargain_info = [
                'remain_entry_total'=>$remain_entry_total,
                'runing'=>$runing,
                'set_entry_total'=>$set_entry_total,
                'tips'=>$tips
            ];
            //基础信息
            $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
            $get_bargain_order_flag = Bargain::getAllBargainFlagListLimit($themeid);
            //获取当前活动的砍价记录
            $bargain_order_flag = Bargain::getFormatBargainOrderFlag($get_bargain_order_flag);
            $format_res = [
                'error_text'=>$error_text,
                'default_info'=>$default_info,
                'display_bargain_info'=>$display_bargain_info,
                'bargain_order_flag'=>$bargain_order_flag,
                'bargain_ranking'=>$bargain_ranking,
                'user_icon_list'=>$user_icon_list,
                'order_bargain_info'=>[],
                'help_bargain_info'=>[],
                'help_todo_bargain_info'=>[],
                'return_op'=>'display'
            ];
        }else{
            //display
            $tips="立即发起砍价";
            //如果有库存则开启限量购买
            if ($set_entry_total>0){
                //剩余数量
                if ($remain_entry_total==0){
                    $tips='活动太火爆，已被抢空了';
                    $runing=100;
                }else{
                    $runing = intval($success_total/$set_entry_total*100);
                }
            }
            $display_bargain_info = [
                'remain_entry_total'=>$remain_entry_total,
                'runing'=>$runing,
                'set_entry_total'=>$set_entry_total,
                'tips'=>$tips
            ];
            //基础信息
            $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
            $get_bargain_order_flag = Bargain::getAllBargainFlagListLimit($themeid);
            //获取当前活动的砍价记录
            $bargain_order_flag = Bargain::getFormatBargainOrderFlag($get_bargain_order_flag);
            $format_res = [
                'error_text'=>$error_text,
                'default_info'=>$default_info,
                'display_bargain_info'=>$display_bargain_info,
                'bargain_order_flag'=>$bargain_order_flag,
                'bargain_ranking'=>$bargain_ranking,
                'user_icon_list'=>$user_icon_list,
                'order_bargain_info'=>[],
                'help_bargain_info'=>[],
                'help_todo_bargain_info'=>[],
                'return_op'=>'display',
                'bargain_limittime'=>$config['bargain_limittime']
            ];
        }
    }elseif ($op=='help_todo'){
        $self_help = false;//是否为自己砍价 黄金宝刀 分享 二次分享
        if ($op_type=='system' ||$op_type=='share' || $op_type=='share_double'){
            $self_help = true;
            $from_uid = $uid;
        }
        //好友或者自己帮砍
        $set_check = checkSetBargainParams($config);//检测砍价设置
        if ($set_check!=true){
            $error_text = $set_check;
        }
        //判断砍价实例是否存在
        $has_entry = Bargain::checkBargainEntryByOrderId($order_id,$from_uid,$themeid);
        if (!$has_entry){
            $error_text = modelShow(40062);
        }
        //判断砍价实例状态
        $entry_status = Bargain::checkBargainStatus($system,$theme,$has_entry);
        if ($entry_status!=1){//不合法状态
            $error_text = Bargain::formatBargainStatus($entry_status);
        }
        //判断助力人是否存在本砍价实例的助力信息
        $has_current_flag = Bargain::checkUserHasBargainFlag($themeid,$uid,$order_id);
        //当前助力人的总帮砍记录数
        //$bargain_help_flag = Bargain::getUserBargainNum($themeid,$uid);
        $bargain_friend_help_flag = Bargain::getUserBargainFriendNum($themeid,$uid);
        if ($has_current_flag && $op_type=='friend'){
            //已经助力过
            $error_text=modelShow(40068);
        }
        //判断好友帮砍限制
//        if ($config['bargain_limitfriend']==0 && $op_type=='friend'){
//            $error_text=modelShow(40071);
//        }
        if ($config['bargain_limitfriend']!=0 && $config['bargain_limitfriend']<=$bargain_friend_help_flag && $op_type=='friend'){
            //达到好友帮砍上限
            $error_text=modelShow(40069);
        }
        //判断宝刀  分享  二次分享加倍
        if ($config['bargain_self_grid']==0 && $op_type=='system' && $from_uid == $uid){
            $error_text=modelShow(40072);
        }
        if ($config['bargain_self_share']==0 && $op_type=='share' && $from_uid == $uid){
            $error_text=modelShow(40073);
        }
        if ($config['bargain_self_dshare']==0 && $op_type=='share_double' && $from_uid == $uid){
            $error_text=modelShow(40074);
        }
        if ($self_help){
            //判断宝刀 分享微信群  翻倍 是否已经存在帮砍记录
            //黄金宝刀
            $has_grid = Bargain::hasTypeFlag($themeid,$order_id,$uid,'system');
            //分享砍价
            $has_share = Bargain::hasTypeFlag($themeid,$order_id,$uid,'share');
            //在分享加倍
            $has_share_double = Bargain::hasTypeFlag($themeid,$order_id,$uid,'share_double');
            if ($op_type=='system' &&  $from_uid == $uid && $has_grid){
                $error_text=modelShow(40075);
            }
            if ($op_type=='share' && $from_uid == $uid && $has_share){
                $error_text=modelShow(40076);
            }
            if ($op_type=='share_double' && $from_uid == $uid && $has_share_double){
                $error_text=modelShow(40077);
            }
        }

        //判断帮砍人是否发起宝刀等操作
        if (($op_type=='system' || $op_type=='share'||$op_type=='share_double') && $from_uid != $uid){
            $error_text=modelShow(40078);
        }
        $help_todo_bargain_info = [];
        //判断砍价模式
        $bargain_type = Bargain::getBargainType($config);
        if (empty($error_text)){
            if (empty($op_type)){
                $get_rand_amount = Bargain::getUserUsedAmount($themeid,$fans,$from_uid,$order_id,$bargain_type,$config);
            }
            //好友帮砍
            if ($op_type=='friend'){
                $get_rand_amount = Bargain::getUserUsedAmount($themeid,$fans,$from_uid,$order_id,$bargain_type,$config);
            }
            //自主分享
            if ($op_type=='share' && $from_uid == $uid){
                $used_share_double = false;
                $where = ['order_id'=>$order_id];
                $data = ['show_share'=>'yes'];
                $update_grid_show = Bargain::updateBargainEntry($where,$data);
                $get_rand_amount = Bargain::getUserUsedAmount($themeid,$fans,$from_uid,$order_id,$bargain_type,$config,'share');
            }
            //黄金宝刀
            if ($op_type=='system' && $from_uid == $uid){
                $used_share = true;
                $used_share_double = true;
                $get_rand_amount = Bargain::getUserUsedAmount($themeid,$fans,$from_uid,$order_id,$bargain_type,$config,'system');
                //更新宝刀提示已显示
                $where = ['order_id'=>$order_id];
                $data = ['show_grid'=>'yes'];
                $update_grid_show = Bargain::updateBargainEntry($where,$data);
            }
            //在分享 上一次分享的加倍
            if ($op_type=='share_double' && $from_uid == $uid && $b_id){
                $where = ['order_id'=>$order_id];
                $data = ['show_share_double'=>'yes'];
                $update_grid_show = Bargain::updateBargainEntry($where,$data);
                $get_rand_amount = Bargain::getUserUsedAmount($themeid,$fans,$from_uid,$order_id,$bargain_type,$config,'share_double',$b_id);
            }

            $new_entry = Bargain::checkBargainEntryByOrderId($order_id,$from_uid,$themeid);
            //判断定制邀请好友有礼
            if ($system['bargain_gift']==1 && $op_type=='friend'){
                //减人数 判断状态
                $update_bargain_entry = Bargain::checkBargainEntryGift($themeid,$order_id);
            }
            if ($get_rand_amount['get_rand_float']){

                //判断帮砍人的砍价实例
                $help_friend_has_bargain = false;
                $help_friend_bargain = Bargain::getUserProcessBargainEntry($themeid,$uid);

                if ($help_friend_bargain){
                    $help_friend_has_bargain = true;
                }
                $help_todo_bargain_info = [
                    'get_rand_amount'=>$get_rand_amount['get_rand_float'],
                    'b_id'=>$get_rand_amount['b_id'],
                    'remain_amount'=>$new_entry['remain_amount'],
                ];
                $help_todo_bargain_info['help_friend_has_bargain'] = $help_friend_has_bargain;
                $help_todo_bargain_info['help_friend_bargain'] = $help_friend_bargain;

                $bargain_is_debug = false;
                if ($bargain_is_debug==true){
                    //判断是否为砍价发起者 调用订阅消息
                    $openid = Bargain::getUserOpenidByUid($from_uid)['openid'];
                    $appid = $system['appid'];
                    $appsecret = $system['secret'];
                    $template_id = $_P['template_id'];
                    $page = $_P['page'];
                    $orderby_num = Bargain::getCurrentFlagInAllFlag($get_rand_amount['b_id'],$from_uid);//当时是第几条记录
                    $tpl_ids = Bargain::getBargainWxSendTmpIds();

                    $now_num = 0;
                    if ($orderby_num<=3){
                        $now_num = $orderby_num-1;
                    }else{
                        $now_num = ($orderby_num%3)-1;
                    }
                    //判断砍价成功
                    if ($new_entry['remain_amount']<=$config['bargain_minprice']){
                        $tpl_id = $tpl_ids[3];
                        $help_bargain_flag = count(Bargain::getUserBargainFlagList($order_id,$from_uid,$themeid));//总共助力次数
                    }else{
                        $tpl_id = $tpl_ids[$now_num];
                    }

                    switch ($now_num){
                        case 0:
                            $res_data = [
                                'thing1'=>'好友帮砍掉'.$get_rand_amount['get_rand_float'].'元',//砍价结果
                                'thing2'=>$theme['title'],//活动名称
                                'amount3'=>$new_entry['remain_amount'].'元',//砍后底价
                                'date4'=>$new_entry['end_time'],//有效期
                                'thing5'=>'点击邀请好友帮砍',//温馨提示
                            ];
                            break;
                        case 1:
                            $res_data = [
                                'phrase1'=>'恭喜邀请好友帮砍成功',//邀请结果  恭喜邀请成功
                                'thing3'=>'砍掉'.$get_rand_amount['get_rand_float'].'元',//哟请奖励   通用优惠券*3 云钻*20
                                'date4'=>$new_entry['end_time'],//有效期      2019-12-31 23:59:59
                                'thing5'=>'点击邀请好友帮砍',//温馨提示    点击查看收到的奖励
                            ];
                            break;
                        case 2:
                            $res_data = [
                                'thing1'=>'好友帮砍掉'.$get_rand_amount['get_rand_float'].'元',//备注    你又获得了内购券
                                'thing2'=>'砍掉一刀',//变动数量 获得2张
                                'thing3'=>'好友助力帮砍成功',//变动原因 xxx等好友帮你助力
                                'date4'=>date("Y-m-d H:i:s",time()),//变动时间
                            ];
                            break;
                        case 3:
//                        $res_data = [
//                            'number1'=>$help_bargain_flag.'人次',//助力人数
//                            'time2'=>date("Y-m-d H:i:s",time()),//成功时间
//                            'thing3'=>'您的砍价已成功砍至最低价',//温馨提示
//                            'thing4'=>$theme['title'],//活动商品
//                        ];
                            $res_data = [
                                'thing1'=>'您的砍价已成功砍至最低价',//砍价结果
                                'thing2'=>$theme['title'],//活动名称
                                'amount3'=>$new_entry['remain_amount'].'元',//砍后底价
                                'date4'=>$new_entry['end_time'],//有效期
                                'thing5'=>'您的砍价已成功,进入查看',//温馨提示
                            ];
                            break;
                        default:
                            break;

                    }

                    $postData = [
                        'appid'=>$appid,
                        'appsecret'=>$appsecret,
                        'touser'=>$openid,
                        'template_id'=>$tpl_id,
                        'page'=>'/pages/argain/bargain/',
                        'res_data'=>$res_data
                    ];
                    WxMessage::sendMessage($postData);
                }

            }
        }



        //获取帮砍记录
        $get_bargain_order_flag = Bargain::getUserNotShareBargainFlagList($order_id,$from_uid,$themeid);
        $bargain_order_flag = Bargain::getFormatBargainOrderFlag($get_bargain_order_flag);

        if ($self_help){
            //黄金宝刀
            $has_grid = Bargain::hasTypeFlag($themeid,$order_id,$uid,'system');
            //分享砍价
            $has_share = Bargain::hasTypeFlag($themeid,$order_id,$uid,'share');
            //在分享加倍
            $has_share_double = Bargain::hasTypeFlag($themeid,$order_id,$uid,'share_double');
            if ($has_grid){
                $used_grid = false;
            }else{
                if ($config['bargain_self_grid']==1 && $has_entry['show_grid']=='no'){
                    $used_grid = true;
                }
            }
            if ($has_share){
                $used_share = false;
            }
            if ($has_share_double){
                $used_share_double = false;
            }
        }else{
            $used_grid = false;
            $used_share = false;
            $used_share_double = false;
        }

        //判断当前砍价实例是否完成或者失效
        if ($has_entry['status']==1 || $has_entry['status']==3){
            $used_grid = false;
            $used_share = false;
            $used_share_double = false;
        }
        //判断是否是定制 曦尊
        if ($system['bargain_gift']==1){
            $used_grid = false;
        }


        //判断砍价实例的状态
        $check_entry_status = Bargain::checkBargainStatus($system,$theme,$has_entry);
        if ($check_entry_status!=1 && $check_entry_status!=5 && $check_entry_status!=6){
            if ($check_entry_status==2){
                $error_text = modelShow(40070);
            }
            if ($check_entry_status==3){
                $error_text = modelShow(40064);
            }
            if ($check_entry_status==4){
                $error_text = modelShow(40065);
            }
        }

        $return_arr = Bargain::getHelpArr($themeid,$uid,$from_uid,$order_id,$config,$check_entry_status,$has_entry);

        $default_info = Bargain::getDefaultArr($config,$theme,$resal_theme_help_total,$success_total,$remain_entry_total,$mode,$is_new_order);
        //获取已砍价用户头像列表
        $user_icon_list = Bargain::getRealAndVirtualInfo($config,$themeid);//他们都在砍
        $format_res = [
            'error_text'=>$error_text,
            'default_info'=>$default_info,
            'bargain_order_flag'=>$bargain_order_flag,
            'bargain_ranking'=>$bargain_ranking,
//            'help_bargain_info'=>$return_arr,
            'help_todo_bargain_info'=>$help_todo_bargain_info,
            'grid_tips'=>($config['bargain_self_grid']==1 && $used_grid==true)?$grid_tips:'',
            'user_icon_list'=>$user_icon_list,
            'self_help'=>$self_help,
            'return_op'=>$op,
            'use_grid'=>$used_grid,
            'use_share'=>$used_share,
            'use_share_double'=>$used_share_double
        ];
        if ($self_help){
            //重新获取砍价实例
            $has_entry = Bargain::checkBargainEntryByOrderId($has_entry['order_id'],$from_uid,$themeid);
            $return_order_arr = Bargain::getOrderArr($themeid,$order_id,$uid,$has_entry,$system,$config,$check_entry_status,$remain_entry_total);
            $format_res['order_bargain_info'] = $return_order_arr['order_bargain_info'];
        }
    }elseif ($op=='bargain_gift'){
        //曦尊领取礼品
        $option_bargain_gift_msg = '';
        if ($system['bargain_gift']==1){
            $has_entry = $bargain_entry = Bargain::checkBargainEntryByOrderId($order_id,$uid,$themeid);
            if ($bargain_entry['bargain_gift']=='success' && !$bargain_entry['bargain_gift_remain_num']){
                $where = ['order_id'=>$order_id,'uid'=>$uid,'themeid'=>$themeid];
                $data = ['bargain_gift'=>'receive'];
                $update_item = Bargain::updateBargainEntry($where,$data);
                $option_bargain_gift_msg = '礼品领取成功,请到店核销兑换';
            }
            $limit = $has_entry['bargain_gift_remain_num'];
            $bargain_gift_status = $has_entry['bargain_gift'];
            $desc = '邀请'.$limit.'位好友帮砍，即可领取'.$config['bargain_gift_name'];
            if ($bargain_gift_status=='process' && $limit>0){
                $desc = '邀请'.$limit.'位好友帮砍，即可领取'.$config['bargain_gift_name'];
            }elseif ($bargain_gift_status=='success' && $limit==0){
                $desc = '邀请好友帮砍完成,'.'可领取'.$config['bargain_gift_name'];
            }elseif ($bargain_gift_status=='receive' && $limit==0){
                $desc = '礼品领取成功，请到店核销兑换';
            }elseif ($bargain_gift_status=='verification'){
                $desc ='礼品已核销兑换';
            }

            $grid_item = ['title'=>'砍价邀请好友有礼','img'=>'/images/g3.png','status'=>2,
                'btnTxt'=>'去完成','desc'=>$desc,'b_id'=>0,'op_type'=>'bargain_gift','item_code'=>$has_entry['bargain_gift']];
            if ($remain_entry_total>0){
                if ($has_entry['bargain_gift']=='success'){
                    $grid_item['status']=1;
                    $grid_item['btnTxt']='待领取';
                    $show_bargain_gift_modal = true;
                }elseif ($has_entry['bargain_gift']=='receive'){
                    $grid_item['status']=1;
                    $grid_item['btnTxt']='已领取';
                }else{
                    if ($bargain_entry['status']==3){
                        $grid_item['status']=1;
                        $grid_item['btnTxt']='砍价失效';
                    }
                }
            }else{
                if ($has_entry['bargain_gift']=='success'){
                    $grid_item['status']=1;
                    $grid_item['btnTxt']='待领取';
                    $show_bargain_gift_modal = true;
                }elseif ($has_entry['bargain_gift']=='receive'){
                    $grid_item['status']=1;
                    $grid_item['btnTxt']='已领取';
                }else{
                    $grid_item['status']=3;
                    $grid_item['btnTxt']='已抢空';
                }
            }
            array_push($method_list,$grid_item);
            $bargain_gift = [
                'bargain_gift_img'=>$config['bargain_gift_img'],
                'bargain_gift_limit'=>intval($limit),
                'bargain_gift_name'=>$config['bargain_gift_name'],
                'bargain_buy'=>$config['bargain_buy'],
                'bargain_buy_text'=>$config['bargain_buy_text'],
                'bargain_order_text'=>$config['bargain_order_text'],
                'desc'=>$desc,
                'return_op'=>$op
            ];
            $format_res = [
                'bargain_gift'=>$bargain_gift,
                'option_bargain_gift_msg'=>$option_bargain_gift_msg
            ];
        }
    }
    $response = modelShow(200,'',$format_res);
}
$this->result(0, '',$response);
}
}